package cn.maiyar.generator.controller;

import cn.hutool.core.util.StrUtil;
import cn.maiyar.common.entity.Query;
import cn.maiyar.generator.service.IDatasourceService;
import cn.maiyar.mybatis.Condition;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import cn.maiyar.web.BaseController;
import cn.maiyar.common.api.Result;
import cn.maiyar.generator.entity.Datasource;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 数据源配置表 控制器
 *
 * @author c.c
 */
@RestController
@AllArgsConstructor
@Api(value = "数据源配置表", tags = "数据源配置表接口")
public class DatasourceController extends BaseController {

    private IDatasourceService datasourceService;

    /**
     * 详情
     */
    @GetMapping("/datasource/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入datasource")
    public Result<Datasource> detail(Datasource datasource) {
        Datasource detail = datasourceService.getOne(Condition.getQueryWrapper(datasource));
        return Result.data(detail);
    }

    /**
     * 分页 数据源配置表
     */
    @GetMapping("/datasource/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入datasource")
    public Result<IPage<Datasource>> list(Datasource datasource, Query query) {
        IPage<Datasource> pages = datasourceService.page(Condition.getPage(query), Condition.getQueryWrapper(datasource));
        return Result.data(pages);
    }

    /**
     * 新增 数据源配置表
     */
    @PostMapping("/datasource/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入datasource")
    public Result<?> save(@Valid @RequestBody Datasource datasource) {
        return Result.status(datasourceService.save(datasource));
    }

    /**
     * 修改 数据源配置表
     */
    @PostMapping("/datasource/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入datasource")
    public Result<?> update(@Valid @RequestBody Datasource datasource) {
        return Result.status(datasourceService.updateById(datasource));
    }

    /**
     * 新增或修改 数据源配置表
     */
    @PostMapping("/datasource/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入datasource")
    public Result<?> submit(@Valid @RequestBody Datasource datasource) {
        datasource.setUrl(datasource.getUrl().replace("&amp;", "&"));
        return Result.status(datasourceService.saveOrUpdate(datasource));
    }


    /**
     * 删除 数据源
     *
     * @param ids
     * @return
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public Result<?> remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		List<String> split = StrUtil.split(ids, StringPool.COMMA);
		return Result.status(datasourceService.deleteLogic(split));
    }

    /**
     * 数据源列表
     */
    @GetMapping("/datasource/select")
    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "下拉数据源", notes = "查询列表")
    public Result<List<Datasource>> select() {
        List<Datasource> list = datasourceService.list();
        return Result.data(list);
    }
}
